<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha256-wiz7ZSCn/btzhjKDQBms9Hx4sSeUYsDrTLg7roPstac=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.css" integrity="sha256-6cQIC71/iBIYXFK+0RHAvwmjwWzkWd+r7v/BX3/vZDc=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/green/pace-theme-minimal.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous"></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"sumumm.github.io","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.19.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":true,"height":300},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="了解下I2C-Tools。若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:type" content="article">
<meta property="og:title" content="LV08-01-I2C子系统-02-I2CTools">
<meta property="og:url" content="https://sumumm.github.io/post/e2cf4441.html">
<meta property="og:site_name" content="苏木">
<meta property="og:description" content="了解下I2C-Tools。若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV08-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F/LV08-01-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F-02-I2CTools/img/image-20250324193337385.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV08-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F/LV08-01-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F-02-I2CTools/img/image-20250324195139434.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV08-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F/LV08-01-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F-02-I2CTools/img/030_i2ctools_and_i2c_dev.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV08-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F/LV08-01-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F-02-I2CTools/img/image-20250324233757890.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV08-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F/LV08-01-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F-02-I2CTools/img/image-20250324234208125.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV08-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F/LV08-01-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F-02-I2CTools/img/image-20250324234306894.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV08-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F/LV08-01-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F-02-I2CTools/img/image-20250324234940023.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV08-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F/LV08-01-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F-02-I2CTools/img/image-20250324235328001.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV08-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F/LV08-01-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F-02-I2CTools/img/image-20250324235435680.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV08-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F/LV08-01-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F-02-I2CTools/img/image-20250324235723228.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV08-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F/LV08-01-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F-02-I2CTools/img/image-20250325000439916.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV08-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F/LV08-01-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F-02-I2CTools/img/image-20250325000549469.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV08-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F/LV08-01-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F-02-I2CTools/img/031_i2ctransfer_flow.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV08-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F/LV08-01-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F-02-I2CTools/img/032_i2cget_i2cset_flow.png">
<meta property="article:published_time" content="2025-04-07T23:38:00.000Z">
<meta property="article:modified_time" content="2025-06-13T16:25:57.057Z">
<meta property="article:author" content="苏木">
<meta property="article:tag" content="LV08-I2C子系统">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV08-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F/LV08-01-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F-02-I2CTools/img/image-20250324193337385.png">


<link rel="canonical" href="https://sumumm.github.io/post/e2cf4441.html">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":"","permalink":"https://sumumm.github.io/post/e2cf4441.html","path":"post/e2cf4441.html","title":"LV08-01-I2C子系统-02-I2CTools"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>LV08-01-I2C子系统-02-I2CTools | 苏木</title>
  








    <script src="/js/browser_tools_disable.js"></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<!-- hexo injector head_end start --><link rel="stylesheet" href="https://unpkg.com/hexo-next-tags-plus@latest/lib/tag_plus.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">苏木</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">我的学习之路</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>苏木的家</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类页<span class="badge">42</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档页<span class="badge">673</span></a></li><li class="menu-item menu-item-flink"><a href="/flink/" rel="section"><i class="fa fa-link fa-fw"></i>友人帐</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于我</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81%E5%8F%82%E8%80%83%E8%B5%84%E6%96%99"><span class="nav-text">一、参考资料</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-Linux%E9%A9%B1%E5%8A%A8%E7%A8%8B%E5%BA%8F"><span class="nav-text">1. Linux驱动程序</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-I2C-Tools"><span class="nav-text">2. I2C-Tools</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81I2C-Tools%E7%AE%80%E4%BB%8B"><span class="nav-text">二、I2C-Tools简介</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-I2C-Tools%E6%98%AF%E4%BB%80%E4%B9%88%EF%BC%9F"><span class="nav-text">1. I2C-Tools是什么？</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-I2C-Tools%E7%9A%84%E8%AE%BF%E9%97%AEI2C%E8%AE%BE%E5%A4%87%E7%9A%842%E7%A7%8D%E6%96%B9%E5%BC%8F"><span class="nav-text">2. I2C-Tools的访问I2C设备的2种方式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E4%BA%A4%E5%8F%89%E7%BC%96%E8%AF%91"><span class="nav-text">3. 交叉编译</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%89%E3%80%81%E4%BD%BF%E7%94%A8I2C-Tools"><span class="nav-text">三、使用I2C-Tools</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-i2cdetect%EF%BC%9AI2C%E6%A3%80%E6%B5%8B"><span class="nav-text">1. i2cdetect：I2C检测</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-%E4%BD%BF%E7%94%A8%E8%AF%B4%E6%98%8E"><span class="nav-text">1.1 使用说明</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">1.2 使用实例</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-i2cget%EF%BC%9AI2C%E8%AF%BB"><span class="nav-text">2. i2cget：I2C读</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-%E4%BD%BF%E7%94%A8%E8%AF%B4%E6%98%8E%E5%A6%82%E4%B8%8B"><span class="nav-text">2.1 使用说明如下</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-%E4%BD%BF%E7%94%A8%E7%A4%BA%E4%BE%8B"><span class="nav-text">2.2 使用示例</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-i2cset%EF%BC%9AI2C%E5%86%99"><span class="nav-text">3. i2cset：I2C写</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1-%E4%BD%BF%E7%94%A8%E8%AF%B4%E6%98%8E"><span class="nav-text">3.1 使用说明</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2-%E4%BD%BF%E7%94%A8%E7%A4%BA%E4%BE%8B"><span class="nav-text">3.2 使用示例</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-i2ctransfer%EF%BC%9AI2C%E4%BC%A0%E8%BE%93-%E4%B8%8D%E6%98%AF%E5%9F%BA%E4%BA%8ESMBus"><span class="nav-text">4. i2ctransfer：I2C传输(不是基于SMBus)</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#4-1-%E4%BD%BF%E7%94%A8%E8%AF%B4%E6%98%8E"><span class="nav-text">4.1 使用说明</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-2-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">4.2 使用实例</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%9B%9B%E3%80%81AP3216C%E7%9A%84%E6%93%8D%E4%BD%9C"><span class="nav-text">四、AP3216C的操作</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-AP3216C%E7%AE%80%E4%BB%8B"><span class="nav-text">1. AP3216C简介</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E7%A1%AC%E4%BB%B6%E5%8E%9F%E7%90%86%E5%9B%BE"><span class="nav-text">2. 硬件原理图</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E8%AE%BE%E5%A4%87%E5%9C%B0%E5%9D%80"><span class="nav-text">3. 设备地址</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E8%AF%BB%E5%86%99%E6%AD%A5%E9%AA%A4"><span class="nav-text">4. 读写步骤</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-I2C-Tools%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="nav-text">5. I2C-Tools的使用</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#5-1-i2cdetect"><span class="nav-text">5.1 i2cdetect</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-2-%E4%BD%BF%E7%94%A8SMBus%E5%8D%8F%E8%AE%AE"><span class="nav-text">5.2 使用SMBus协议</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-3-%E4%BD%BF%E7%94%A8I2C%E5%8D%8F%E8%AE%AE"><span class="nav-text">5.3 使用I2C协议</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%94%E3%80%81%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90"><span class="nav-text">五、源码分析</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E4%BD%BF%E7%94%A8I2C%E6%96%B9%E5%BC%8F"><span class="nav-text">1. 使用I2C方式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E4%BD%BF%E7%94%A8SMBus%E6%96%B9%E5%BC%8F"><span class="nav-text">2. 使用SMBus方式</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="苏木"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">苏木</p>
  <div class="site-description" itemprop="description">莫道桑榆晚，为霞尚满天</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">673</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">42</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">43</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/sumumm" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;sumumm" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://sumumm.github.io/post/e2cf4441.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="苏木">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏木">
      <meta itemprop="description" content="莫道桑榆晚，为霞尚满天">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="LV08-01-I2C子系统-02-I2CTools | 苏木">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          LV08-01-I2C子系统-02-I2CTools
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2025-04-08 07:38:00" itemprop="dateCreated datePublished" datetime="2025-04-08T07:38:00+08:00">2025-04-08</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">嵌入式开发</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/" itemprop="url" rel="index"><span itemprop="name">02IMX6ULL平台</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV08-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F/" itemprop="url" rel="index"><span itemprop="name">LV08-I2C子系统</span></a>
        </span>
    </span>

  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>4.7k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>17 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><p>了解下I2C-Tools。若笔记中有错误或者不合适的地方，欢迎批评指正😃。</p>
<span id="more"></span>

<!-- Photo: https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV08-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F/LV08-01-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F-02-I2CTools/img/ -->

<details class="folding-tag" blue><summary> 点击查看使用工具及版本 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" rowspan="5">PC端开发环境</td>        <td align="center" width=150px>Windows</td>        <td align="left">Windows11</td>    </tr>    <tr>        <td align="center">Ubuntu</td>        <td align="left">Ubuntu20.04.2的64位版本</td>      </tr>    <tr>        <td align="center">VMware® Workstation 17 Pro</td>        <td align="left">17.6.0 build-24238078</td>      </tr>    <tr>        <td align="center">终端软件</td>        <td align="left">MobaXterm(Professional Edition v23.0 Build 5042 (license))</td>    </tr>    <tr>        <td align="center">Win32DiskImager</td>        <td align="left">Win32DiskImager v1.0</td>      </tr>    <tr>        <td align="center" rowspan="3">Linux开发板环境</td>        <td align="center">Linux开发板</td>        <td align="left">正点原子 i.MX6ULL Linux 阿尔法开发板</td>      </tr>    <tr>        <td align="center">uboot</td>        <td align="left">NXP官方提供的uboot，使用的uboot版本为U-Boot 2019.04</td>      </tr>    <tr>        <td align="center">linux内核</td>        <td align="left">linux-4.19.71(NXP官方提供)</td>      </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看本文参考资料 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center">分类</td>        <td align="center">网址</td>        <td align="center">说明</td>    </tr>    <tr>        <td align="center" rowspan="5">官方网站</td>        <td align="left"><a href="https://www.arm.com/" target="_blank">https://www.arm.com/</a></td>        <td align="left">ARM官方网站，在这里我们可以找到Cotex-Mx以及ARMVx的一些文档</td>    </tr>    <tr>        <td align="left"><a href="https://www.nxp.com.cn/" target="_blank">https://www.nxp.com.cn/ </a></td>        <td align="left">NXP官方网站</td>    </tr>    <tr>        <td align="left"><a href="https://www.nxpic.org.cn/" target="_blank">https://www.nxpic.org.cn/</a></td><td align="left">NXP 官方社区</td>    </tr>    <tr>        <td align="left"><a href="https://u-boot.readthedocs.io/en/latest/" target="_blank">https://u-boot.readthedocs.io/en/latest/</a></td><td align="left">u-boot官网</td>    </tr>    <tr>        <td align="left"><a href="https://www.kernel.org/" target="_blank">https://www.kernel.org/</a></td><td align="left">linux内核官网</td>    </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看相关文件下载 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center">分类</td>        <td align="center">网址</td>        <td align="center">说明</td>    </tr>    <tr>        <td align="center" rowspan="3">NXP</td>        <td align="left"><a href="https://github.com/nxp-imx" target="_blank">https://github.com/nxp-imx</a></td>        <td align="left">NXP imx开发资源GitHub组织，里边会有u-boot和linux内核的仓库</td>    </tr>    <tr>        <td align="left"><a href="https://github.com/nxp-imx/linux-imx/releases/tag/v4.19.71" target="_blank">nxp-imx/linux-imx/releases/tag/v4.19.71</a></td>        <td align="left">NXP linux内核仓库tags中的v4.19.71</td>    </tr>    <tr>        <td align="left"><a href="https://github.com/nxp-imx/uboot-imx/releases/tag/rel_imx_4.19.35_1.1.0" target="_blank">nxp-imx/uboot-imx/releases/tag/rel_imx_4.19.35_1.1.0</a></td>        <td align="left">NXP u-boot仓库tags中的rel_imx_4.19.35_1.1.0</td>    </tr>    <tr>        <td align="center" rowspan="2">I.MX6ULL</td>        <td align="left"><a href="https://www.nxp.com.cn/docs/en/data-sheet/IMX6ULLIEC.pdf" target="_blank">i.MX 6ULL Applications Processors for Industrial Products</a></td>        <td align="left">I.MX6ULL 芯片手册（datasheet，可以在线查看）</td>    </tr>    <tr>        <td align="left"><a href="https://www.nxp.com.cn/webapp/Download?colCode=IMX6ULLRM&lang_cd=zh" target="_blank">i.MX 6ULL Applications ProcessorReference Manual</a></td>        <td align="left">I.MX6ULL 参考手册（下载后才能查看，需要登录NXP官网）</td>    </tr>    <tr>        <td align="center" rowspan="3">Source Code</td>        <td align="left"><a href="https://elixir.bootlin.com/linux/latest/source" target="_blank">https://elixir.bootlin.com/linux/latest/source</a></td>        <td align="left">linux kernel源码</td>    </tr>    <tr>        <td align="left"><a href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4" target="_blank">kernel/git/stable/linux.git - Linux kernel stable tree</a></td>        <td align="left">linux kernel源码(官网,tag 4.19.71)</td>    </tr>    <tr>        <td align="left"><a href="https://elixir.bootlin.com/u-boot/latest/source" target="_blank">https://elixir.bootlin.com/u-boot/latest/source</a></td>        <td align="left">uboot源码</td>    </tr></table>
              </div>
            </details>

<h1 id="一、参考资料"><a href="#一、参考资料" class="headerlink" title="一、参考资料"></a><font size=3>一、参考资料</font></h1><h2 id="1-Linux驱动程序"><a href="#1-Linux驱动程序" class="headerlink" title="1. Linux驱动程序"></a><font size=3>1. Linux驱动程序</font></h2><p> <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/i2c/i2c-dev.c">i2c-dev.c - drivers&#x2F;i2c&#x2F;i2c-dev.c</a></p>
<h2 id="2-I2C-Tools"><a href="#2-I2C-Tools" class="headerlink" title="2. I2C-Tools"></a><font size=3>2. I2C-Tools</font></h2><p>这个工具官网在：<a target="_blank" rel="noopener" href="https://archive.kernel.org/oldwiki/i2c.wiki.kernel.org/index.php/I2C_Tools.html">I2C Tools - Linux i2c Wiki</a></p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV08-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F/LV08-01-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F-02-I2CTools/img/image-20250324193337385.png" alt="image-20250324193337385"  />

<p>github仓库在这里：<a target="_blank" rel="noopener" href="https://github.com/oudream/i2c-tools">GitHub - oudream&#x2F;i2c-tools</a></p>
<p>下载页面在这里：<a target="_blank" rel="noopener" href="https://www.kernel.org/pub/software/utils/i2c-tools/">Index of &#x2F;pub&#x2F;software&#x2F;utils&#x2F;i2c-tools&#x2F;</a></p>
<h1 id="二、I2C-Tools简介"><a href="#二、I2C-Tools简介" class="headerlink" title="二、I2C-Tools简介"></a><font size=3>二、I2C-Tools简介</font></h1><h2 id="1-I2C-Tools是什么？"><a href="#1-I2C-Tools是什么？" class="headerlink" title="1. I2C-Tools是什么？"></a><font size=3>1. I2C-Tools是什么？</font></h2><p>i2c-tools工具是一个专门调试i2c的，开源，可获取挂载的设备及设备地址，还可以在对应的设备指定寄存器设置值或者获取值等功能。通过这个工具，我们无需编写驱动程序即可访问I2C设备。</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV08-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F/LV08-01-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F-02-I2CTools/img/image-20250324195139434.png" alt="image-20250324195139434"  />

<p>对于APP访问硬件肯定是需要驱动程序的，对于I2C设备，内核提供了驱动程序 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/i2c/i2c-dev.c">i2c-dev.c - drivers&#x2F;i2c&#x2F;i2c-dev.c</a>，通过它可以直接使用下面的I2C控制器驱动程序来访问I2C设备。基本框架如下：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV08-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F/LV08-01-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F-02-I2CTools/img/030_i2ctools_and_i2c_dev.png" alt="image-20210224172517485"  />

<p>i2c-tools是一套好用的工具，也是一套示例代码。</p>
<h2 id="2-I2C-Tools的访问I2C设备的2种方式"><a href="#2-I2C-Tools的访问I2C设备的2种方式" class="headerlink" title="2. I2C-Tools的访问I2C设备的2种方式"></a><font size=3>2. I2C-Tools的访问I2C设备的2种方式</font></h2><p>I2C-Tools可以通过SMBus来访问I2C设备，也可以使用一般的I2C协议来访问I2C设备。使用一句话概括I2C传输：APP通过I2C Controller与I2C Device传输数据。在APP里，有这几个问题：</p>
<ul>
<li>怎么指定I2C控制器？</li>
</ul>
<p>i2c-dev.c提供为每个I2C控制器(I2C Bus、I2C Adapter)都生成一个设备节点：&#x2F;dev&#x2F;i2c-0、&#x2F;dev&#x2F;i2c-1等</p>
<p>open某个&#x2F;dev&#x2F;i2c-X节点，就是去访问该I2C控制器下的设备</p>
<ul>
<li>怎么指定I2C设备？通过ioctl指定I2C设备的地址</li>
</ul>
<p>（1）I2C_SLAVE</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ioctl(file,  I2C_SLAVE, address)</span><br></pre></td></tr></table></figure>

<p>如果该设备已经有了对应的设备驱动程序，则返回失败。</p>
<p>（2）I2C_SLAVE_FORCE</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ioctl(file,  I2C_SLAVE_FORCE, address)</span><br></pre></td></tr></table></figure>

<p>如果该设备已经有了对应的设备驱动程序，但是还是想通过i2c-dev驱动来访问它，则使用这个ioctl来指定I2C设备地址。</p>
<ul>
<li>怎么传输数据？</li>
</ul>
<p>（1）一般的I2C方式：<code>ioctl(file, I2C_RDWR, &amp;rdwr)</code></p>
<p>（2）SMBus方式：<code>ioctl(file, I2C_SMBUS, &amp;args)</code></p>
<h2 id="3-交叉编译"><a href="#3-交叉编译" class="headerlink" title="3. 交叉编译"></a><font size=3>3. 交叉编译</font></h2><ul>
<li>设置交叉编译工具</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">export ARCH=arm</span><br><span class="line">export CROSS_COMPILE=arm-linux-gnueabihf-</span><br><span class="line">export PATH=$PATH:/home/sumu/2software/gcc-arm-linux-gnueabihf-8.3.0/bin</span><br></pre></td></tr></table></figure>

<ul>
<li>修改I2C-Tools的Makefile指定交叉编译工具链</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">CC      ?= gcc</span><br><span class="line">AR      ?= ar</span><br><span class="line">STRIP   ?= strip</span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">改为(指定交叉编译工具链前缀, 去掉问号)：</span></span><br><span class="line">CC      = $(CROSS_COMPILE)gcc</span><br><span class="line">AR      = $(CROSS_COMPILE)ar</span><br><span class="line">STRIP   = $(CROSS_COMPILE)strip</span><br></pre></td></tr></table></figure>

<p>在Makefile中，“?&#x3D;”在第一次设置变量时才会起效果，如果之前设置过该变量，则不会起效果。</p>
<ul>
<li>执行make即可。</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">make</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">或者我们之前添加过交叉编译工具刀环境变量的话，可以直接执行以下命令</span></span><br><span class="line">make cc=arm-linux-gnueabihf-gcc AR=arm-linux-gnueabihf-ar STRIP=arm-linux-gnueabihf-strip</span><br></pre></td></tr></table></figure>

<p>执行make时，是动态链接，需要把<code>libi2c.so</code>也放到单板上：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo cp -avf libi2c.so libi2c.so.0 libi2c.so.0.1.1 ~/4nfs/imx6ull_rootfs/lib/</span><br></pre></td></tr></table></figure>

<p>想静态链接的话，执行：<code>make USE_STATIC_LIB=1</code>。</p>
<h1 id="三、使用I2C-Tools"><a href="#三、使用I2C-Tools" class="headerlink" title="三、使用I2C-Tools"></a><font size=3>三、使用I2C-Tools</font></h1><p>使用一句话概括I2C传输：APP通过 I2C Controller 与 I2C Device传输数据。所以使用 I2C-Tools 时也需要指定：</p>
<ul>
<li>哪个I2C控制器(或称为I2C BUS、I2C Adapter)</li>
<li>哪个I2C设备(设备地址)</li>
<li>数据：读还是写、数据本身</li>
</ul>
<h2 id="1-i2cdetect：I2C检测"><a href="#1-i2cdetect：I2C检测" class="headerlink" title="1. i2cdetect：I2C检测"></a><font size=3>1. i2cdetect：I2C检测</font></h2><h3 id="1-1-使用说明"><a href="#1-1-使用说明" class="headerlink" title="1.1 使用说明"></a><font size=3>1.1 使用说明</font></h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">列出当前的I2C Adapter(或称为I2C Bus、I2C Controller)</span></span><br><span class="line">i2cdetect -l</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">打印某个I2C Adapter的Functionalities, I2CBUS为0、1、2等整数</span></span><br><span class="line">i2cdetect -F I2CBUS</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">看看有哪些I2C设备, I2CBUS为0、1、2等整数</span></span><br><span class="line">i2cdetect -y -a I2CBUS</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>注意：i2cdetect 的工作原理是对指定的 I2C 总线上的每个地址发送一个读请求，并根据设备的响应来判断该地址上是否有设备存在。如果设备存在并且响应，i2cdetect 会显示该地址；如果没有响应，则显示 *–*。此外，如果地址已经被内核驱动程序使用，则显示 <em>UU</em></p>
<h3 id="1-2-使用实例"><a href="#1-2-使用实例" class="headerlink" title="1.2 使用实例"></a><font size=3>1.2 使用实例</font></h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">i2cdetect -l</span></span><br><span class="line">i2c-1   i2c             STM32F7 I2C(0x40013000)                 I2C adapter</span><br><span class="line">i2c-2   i2c             STM32F7 I2C(0x5c002000)                 I2C adapter</span><br><span class="line">i2c-0   i2c             STM32F7 I2C(0x40012000)                 I2C adapter</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">i2cdetect -F 0</span></span><br><span class="line">Functionalities implemented by /dev/i2c-0:</span><br><span class="line">I2C                              yes</span><br><span class="line">SMBus Quick Command              yes</span><br><span class="line">SMBus Send Byte                  yes</span><br><span class="line">SMBus Receive Byte               yes</span><br><span class="line">SMBus Write Byte                 yes</span><br><span class="line">SMBus Read Byte                  yes</span><br><span class="line">SMBus Write Word                 yes</span><br><span class="line">SMBus Read Word                  yes</span><br><span class="line">SMBus Process Call               yes</span><br><span class="line">SMBus Block Write                yes</span><br><span class="line">SMBus Block Read                 yes</span><br><span class="line">SMBus Block Process Call         yes</span><br><span class="line">SMBus PEC                        yes</span><br><span class="line">I2C Block Write                  yes</span><br><span class="line">I2C Block Read                   yes</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">i2cdetect -y -a 0</span>  </span><br><span class="line">     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f</span><br><span class="line">00: 00 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --</span><br><span class="line">10: -- -- -- -- -- -- -- -- -- -- UU -- -- -- 1e --</span><br><span class="line">20: -- -- UU -- -- -- -- -- -- -- -- -- -- -- -- --</span><br><span class="line">30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --</span><br><span class="line">40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --</span><br><span class="line">50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --</span><br><span class="line">60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --</span><br><span class="line">70: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">说明：--表示没有该地址对应的设备, UU表示有该设备并且它已经有驱动程序,数值表示有该设备但是没有对应的设备驱动</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="2-i2cget：I2C读"><a href="#2-i2cget：I2C读" class="headerlink" title="2. i2cget：I2C读"></a><font size=3>2. i2cget：I2C读</font></h2><h3 id="2-1-使用说明如下"><a href="#2-1-使用说明如下" class="headerlink" title="2.1 使用说明如下"></a><font size=3>2.1 使用说明如下</font></h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">i2cget</span></span><br><span class="line">Usage: i2cget [-f] [-y] [-a] I2CBUS CHIP-ADDRESS [DATA-ADDRESS [MODE]]</span><br><span class="line">  I2CBUS is an integer or an I2C bus name</span><br><span class="line">  ADDRESS is an integer (0x03 - 0x77, or 0x00 - 0x7f if -a is given)</span><br><span class="line">  MODE is one of:</span><br><span class="line">    b (read byte data, default)</span><br><span class="line">    w (read word data)</span><br><span class="line">    c (write byte/read byte)</span><br><span class="line">    Append p for SMBus PEC</span><br></pre></td></tr></table></figure>

<h3 id="2-2-使用示例"><a href="#2-2-使用示例" class="headerlink" title="2.2 使用示例"></a><font size=3>2.2 使用示例</font></h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">读一个字节: I2CBUS为0、1、2等整数, 表示I2C Bus; CHIP-ADDRESS表示设备地址</span></span><br><span class="line">i2cget -f -y I2CBUS CHIP-ADDRESS</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">读某个地址上的一个字节:</span> </span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">   I2CBUS为0、1、2等整数, 表示I2C Bus</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">   CHIP-ADDRESS表示设备地址</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">   DATA-ADDRESS: 芯片上寄存器地址</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">   MODE：有2个取值, b-使用`SMBus Read Byte`先发出DATA-ADDRESS, 再读一个字节, 中间无P信号</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">                  c-先write byte, 在<span class="built_in">read</span> byte，中间有P信号</span> </span><br><span class="line">i2cget -f -y I2CBUS CHIP-ADDRESS DATA-ADDRESS MODE  </span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">读某个地址上的2个字节:</span> </span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">   I2CBUS为0、1、2等整数, 表示I2C Bus</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">   CHIP-ADDRESS表示设备地址</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">   DATA-ADDRESS: 芯片上寄存器地址</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">   MODE：w-表示先发出DATA-ADDRESS，再读2个字节</span></span><br><span class="line">i2cget -f -y I2CBUS CHIP-ADDRESS DATA-ADDRESS MODE  </span><br></pre></td></tr></table></figure>

<h2 id="3-i2cset：I2C写"><a href="#3-i2cset：I2C写" class="headerlink" title="3. i2cset：I2C写"></a><font size=3>3. i2cset：I2C写</font></h2><h3 id="3-1-使用说明"><a href="#3-1-使用说明" class="headerlink" title="3.1 使用说明"></a><font size=3>3.1 使用说明</font></h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">i2cset</span></span><br><span class="line">Usage: i2cset [-f] [-y] [-m MASK] [-r] [-a] I2CBUS CHIP-ADDRESS DATA-ADDRESS [VALUE] ... [MODE]</span><br><span class="line">  I2CBUS is an integer or an I2C bus name</span><br><span class="line">  ADDRESS is an integer (0x03 - 0x77, or 0x00 - 0x7f if -a is given)</span><br><span class="line">  MODE is one of:</span><br><span class="line">    c (byte, no value)</span><br><span class="line">    b (byte data, default)</span><br><span class="line">    w (word data)</span><br><span class="line">  i (I2C block data)</span><br><span class="line">    s (SMBus block data)</span><br><span class="line">    Append p for SMBus PEC</span><br></pre></td></tr></table></figure>

<h3 id="3-2-使用示例"><a href="#3-2-使用示例" class="headerlink" title="3.2 使用示例"></a><font size=3>3.2 使用示例</font></h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">写一个字节: I2CBUS为0、1、2等整数, 表示I2C Bus; CHIP-ADDRESS表示设备地址</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">          DATA-ADDRESS就是要写的数据</span></span><br><span class="line">i2cset -f -y I2CBUS CHIP-ADDRESS DATA-ADDRESS</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">给address写1个字节(address, value):</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">          I2CBUS为0、1、2等整数, 表示I2C Bus; CHIP-ADDRESS表示设备地址</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">          DATA-ADDRESS: 8位芯片寄存器地址;</span> </span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">          VALUE: 8位数值</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">          MODE: 可以省略，也可以写为b</span></span><br><span class="line">i2cset -f -y I2CBUS CHIP-ADDRESS DATA-ADDRESS VALUE [b]</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">给address写2个字节(address, value):</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">          I2CBUS为0、1、2等整数, 表示I2C Bus; CHIP-ADDRESS表示设备地址</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">          DATA-ADDRESS: 8位芯片寄存器地址;</span> </span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">          VALUE: 16位数值</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">          MODE: w</span></span><br><span class="line">i2cset -f -y I2CBUS CHIP-ADDRESS DATA-ADDRESS VALUE w</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">SMBus Block Write：给address写N个字节的数据</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">  发送的数据有：address, N, value1, value2, ..., valueN</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">  跟`I2C Block Write`相比, 需要发送长度N</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">          I2CBUS为0、1、2等整数, 表示I2C Bus; CHIP-ADDRESS表示设备地址</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">          DATA-ADDRESS: 8位芯片寄存器地址;</span> </span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">          VALUE1~N: N个8位数值</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">          MODE: s</span></span><br><span class="line">i2cset -f -y I2CBUS CHIP-ADDRESS DATA-ADDRESS VALUE1 ... VALUEN s</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">I2C Block Write：给address写N个字节的数据</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">  发送的数据有：address, value1, value2, ..., valueN</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">  跟`SMBus Block Write`相比, 不需要发送长度N</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">          I2CBUS为0、1、2等整数, 表示I2C Bus; CHIP-ADDRESS表示设备地址</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">          DATA-ADDRESS: 8位芯片寄存器地址;</span> </span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">          VALUE1~N: N个8位数值</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">          MODE: i</span></span><br><span class="line">i2cset -f -y I2CBUS CHIP-ADDRESS DATA-ADDRESS VALUE1 ... VALUEN i</span><br></pre></td></tr></table></figure>

<h2 id="4-i2ctransfer：I2C传输-不是基于SMBus"><a href="#4-i2ctransfer：I2C传输-不是基于SMBus" class="headerlink" title="4. i2ctransfer：I2C传输(不是基于SMBus)"></a><font size=3>4. i2ctransfer：I2C传输(不是基于SMBus)</font></h2><h3 id="4-1-使用说明"><a href="#4-1-使用说明" class="headerlink" title="4.1 使用说明"></a><font size=3>4.1 使用说明</font></h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">i2ctransfer</span></span><br><span class="line">Usage: i2ctransfer [-f] [-y] [-v] [-V] [-a] I2CBUS DESC [DATA] [DESC [DATA]]...</span><br><span class="line">  I2CBUS is an integer or an I2C bus name</span><br><span class="line">  DESC describes the transfer in the form: &#123;r|w&#125;LENGTH[@address]</span><br><span class="line">    1) read/write-flag 2) LENGTH (range 0-65535) 3) I2C address (use last one if omitted)</span><br><span class="line">  DATA are LENGTH bytes for a write message. They can be shortened by a suffix:</span><br><span class="line">    = (keep value constant until LENGTH)</span><br><span class="line">    + (increase value by 1 until LENGTH)</span><br><span class="line">    - (decrease value by 1 until LENGTH)</span><br><span class="line">    p (use pseudo random generator until LENGTH with value as seed)</span><br><span class="line"></span><br><span class="line">Example (bus 0, read 8 byte at offset 0x64 from EEPROM at 0x50):</span><br><span class="line"><span class="meta prompt_">  # </span><span class="language-bash">i2ctransfer 0 w1@0x50 0x64 r8</span></span><br><span class="line">Example (same EEPROM, at offset 0x42 write 0xff 0xfe ... 0xf0):</span><br><span class="line"><span class="meta prompt_">  # </span><span class="language-bash">i2ctransfer 0 w17@0x50 0x42 0xff-</span></span><br></pre></td></tr></table></figure>

<h3 id="4-2-使用实例"><a href="#4-2-使用实例" class="headerlink" title="4.2 使用实例"></a><font size=3>4.2 使用实例</font></h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">Example (bus 0, <span class="built_in">read</span> 8 byte at offset 0x64 from EEPROM at 0x50):</span></span><br><span class="line">i2ctransfer -f -y 0 w1@0x50 0x64 r8</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">Example (bus 0, write 3 byte at offset 0x64 from EEPROM at 0x50):</span></span><br><span class="line">i2ctransfer -f -y 0 w9@0x50 0x64 val1 val2 val3</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">Example</span> </span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">first: (bus 0, write 3 byte at offset 0x64 from EEPROM at 0x50)</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">and <span class="keyword">then</span>: (bus 0, <span class="built_in">read</span> 3 byte at offset 0x64 from EEPROM at 0x50)</span></span><br><span class="line">i2ctransfer -f -y 0 w9@0x50 0x64 val1 val2 val3 r3@0x50  </span><br><span class="line">i2ctransfer -f -y 0 w9@0x50 0x64 val1 val2 val3 r3 //如果设备地址不变,后面的设备地址可省略</span><br></pre></td></tr></table></figure>

<h1 id="四、AP3216C的操作"><a href="#四、AP3216C的操作" class="headerlink" title="四、AP3216C的操作"></a><font size=3>四、AP3216C的操作</font></h1><h2 id="1-AP3216C简介"><a href="#1-AP3216C简介" class="headerlink" title="1. AP3216C简介"></a><font size=3>1. AP3216C简介</font></h2><p>正点原子的开发板上有光感芯片AP3216C：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV08-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F/LV08-01-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F-02-I2CTools/img/image-20250324233757890.png" alt="image-20250324233757890" />

<p>I.MX6U-ALPHA 开发板上通过 I2C1 连接了一个三合一环境传感器： AP3216C， AP3216C是由敦南科技推出的一款传感器，其支持环境光强度(ALS)、接近距离(PS)和红外线强度(IR)这三个环境参数检测。该芯片可以通过 IIC 接口与主控制相连，并且支持中断。寄存器也比较简单：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV08-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F/LV08-01-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F-02-I2CTools/img/image-20250324234208125.png" alt="image-20250324234208125" style="zoom: 33%;" />

<p>0X00 这个寄存器是模式控制寄存器，用来设置 AP3216C 的工作模式，一般开始先将其设置为 0X04，也就是先软件复位一次 AP3216C。接下来根据实际使用情况选择合适的工作模式，比如设置为 0X03，也就是开启 ALS+PS+IR。从 0X0A~0X0F 这 6 个寄存器就是数据寄存器，保存着 ALS、 PS 和 IR 这三个传感器获取到的数据值。如果同时打开 ALS、PS 和 IR 则读取间隔最少要 112.5ms，因为 AP3216C 完成一次转换需要 112.5ms。  </p>
<p>我们编写程序会先检测 AP3216C 是否存在，一般的芯片是有个 ID 寄存器，通过读取 ID 寄存器判断 ID 是否正确就可以检测芯片是否存在。但是 AP3216C 没有 ID 寄存器，所以我们就可以通过向寄存器 0X00 写入一个值，然后再读取 0X00 寄存器，判断读出得到值和写入的是否相等，如果相等就表示 AP3216C 存在，否则的话 AP3216C 就不存在。  </p>
<h2 id="2-硬件原理图"><a href="#2-硬件原理图" class="headerlink" title="2. 硬件原理图"></a><font size=3>2. 硬件原理图</font></h2><img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV08-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F/LV08-01-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F-02-I2CTools/img/image-20250324234306894.png" alt="image-20250324234306894" style="zoom:50%;" />

<h2 id="3-设备地址"><a href="#3-设备地址" class="headerlink" title="3. 设备地址"></a><font size=3>3. 设备地址</font></h2><p>AP3216 的设备地址为 0X1E ，这个我们可以看芯片手册：<a target="_blank" rel="noopener" href="https://www.openhacks.com/uploadsproductos/datasheet_my.pdf">datasheet_my.pdf</a>：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV08-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F/LV08-01-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F-02-I2CTools/img/image-20250324234940023.png" alt="image-20250324234940023" />

<h2 id="4-读写步骤"><a href="#4-读写步骤" class="headerlink" title="4. 读写步骤"></a><font size=3>4. 读写步骤</font></h2><p>AP3216C是红外、光强、距离三合一的传感器，以读出光强、距离值为例，步骤如下：</p>
<ul>
<li>复位：往寄存器0写入0x4。</li>
<li>使能：往寄存器0写入0x3。</li>
<li>读光强：读寄存器0xC、0xD得到2字节的光强。</li>
<li>读距离：读寄存器0xE、0xF得到2字节的距离值。</li>
</ul>
<h2 id="5-I2C-Tools的使用"><a href="#5-I2C-Tools的使用" class="headerlink" title="5. I2C-Tools的使用"></a><font size=3>5. I2C-Tools的使用</font></h2><h3 id="5-1-i2cdetect"><a href="#5-1-i2cdetect" class="headerlink" title="5.1 i2cdetect"></a><font size=3>5.1 i2cdetect</font></h3><ul>
<li>列出当前的I2C Adapter(或称为I2C Bus、I2C Controller)</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">i2cdetect -l</span></span><br><span class="line">i2cdetect -l</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV08-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F/LV08-01-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F-02-I2CTools/img/image-20250324235328001.png" alt="image-20250324235328001" />

<ul>
<li>打印某个I2C Adapter的Functionalities, I2CBUS为0、1、2等整数</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">i2cdetect -F I2CBUS</span></span><br><span class="line">i2cdetect -F 0</span><br><span class="line">i2cdetect -F 1</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV08-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F/LV08-01-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F-02-I2CTools/img/image-20250324235435680.png" alt="image-20250324235435680" />

<ul>
<li>看看有哪些I2C设备, I2CBUS为0、1、2等整数</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">i2cdetect -y -a I2CBUS</span></span><br><span class="line">i2cdetect -y -a 0</span><br><span class="line">i2cdetect -y -a 1</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV08-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F/LV08-01-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F-02-I2CTools/img/image-20250324235723228.png" alt="image-20250324235723228" />

<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">-- ：表示没有该地址对应的设备</span><br><span class="line">UU ：表示有该设备并且它已经有驱动程序</span><br><span class="line">数值：表示有该设备但是没有对应的设备驱动</span><br></pre></td></tr></table></figure>

<p>前面我们知道AP3216C接在I2C1上面，这里对应的是i2c-0。</p>
<h3 id="5-2-使用SMBus协议"><a href="#5-2-使用SMBus协议" class="headerlink" title="5.2 使用SMBus协议"></a><font size=3>5.2 使用SMBus协议</font></h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">i2cset -f -y 0 0x1e 0 0x4 # 复位</span><br><span class="line">i2cset -f -y 0 0x1e 0 0x3 # 使能</span><br><span class="line">i2cget -f -y 0 0x1e 0xc w # 读光强</span><br><span class="line">i2cget -f -y 0 0x1e 0xe w # 读距离</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV08-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F/LV08-01-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F-02-I2CTools/img/image-20250325000439916.png" alt="image-20250325000439916" />

<h3 id="5-3-使用I2C协议"><a href="#5-3-使用I2C协议" class="headerlink" title="5.3 使用I2C协议"></a><font size=3>5.3 使用I2C协议</font></h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">i2ctransfer -f -y 0 w2@0x1e 0 0x4 # 复位</span><br><span class="line">i2ctransfer -f -y 0 w2@0x1e 0 0x3 # 使能</span><br><span class="line">i2ctransfer -f -y 0 w1@0x1e 0xc r2 # 读光强</span><br><span class="line">i2ctransfer -f -y 0 w1@0x1e 0xe r2 # 读距离</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV08-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F/LV08-01-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F-02-I2CTools/img/image-20250325000549469.png" alt="image-20250325000549469" />

<h1 id="五、源码分析"><a href="#五、源码分析" class="headerlink" title="五、源码分析"></a><font size=3>五、源码分析</font></h1><h2 id="1-使用I2C方式"><a href="#1-使用I2C方式" class="headerlink" title="1. 使用I2C方式"></a><font size=3>1. 使用I2C方式</font></h2><p>示例代码：i2ctransfer.c</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV08-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F/LV08-01-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F-02-I2CTools/img/031_i2ctransfer_flow.png" alt="image-20210224191404322" style="zoom:50%;" />



<h2 id="2-使用SMBus方式"><a href="#2-使用SMBus方式" class="headerlink" title="2. 使用SMBus方式"></a><font size=3>2. 使用SMBus方式</font></h2><p>示例代码：i2cget.c、i2cset.c</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV08-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F/LV08-01-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F-02-I2CTools/img/032_i2cget_i2cset_flow.png" alt="image-20210224192345075" style="zoom:50%;" />


    </div>

    
    
    

    <footer class="post-footer">




    <div>
        
            <div style="text-align:center;color: #ccc;font-size:14px;">
            ----------本文结束
            <i class="fas fa-fan fa-spin" style="color: #FF1493; font-size: 1rem"></i>
            感谢您的阅读----------
            </div>
        
    </div>





  
  <div class="my_post_copyright"> 
    <p><span>文章标题:</span><a href="/post/e2cf4441.html">LV08-01-I2C子系统-02-I2CTools</a></p>
    <p><span>文章作者:</span><a href="/" title="欢迎访问 《苏木》 的学习笔记">苏木</a></p>
    <p><span>发布时间:</span>2025年04月08日 - 07:38</p>
    <p><span>最后更新:</span>2025年06月14日 - 00:25</p>
    <p><span>原始链接:</span><a href="/post/e2cf4441.html" title="LV08-01-I2C子系统-02-I2CTools">https://sumumm.github.io/post/e2cf4441.html</a></p>
    <p><span>许可协议:</span><i class="fab fa-creative-commons"></i> <a rel="license" href= "https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
  </div>
  


          <div class="post-tags">
              <a href="/tags/LV08-I2C%E5%AD%90%E7%B3%BB%E7%BB%9F/" rel="tag"><i class="fa fa-tag"></i> LV08-I2C子系统</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/post/1c16e473.html" rel="prev" title="LV08-01-I2C子系统-03-I2C驱动框架简介">
                  <i class="fa fa-angle-left"></i> LV08-01-I2C子系统-03-I2C驱动框架简介
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/post/fce0a478.html" rel="next" title="LV08-01-I2C子系统-01-I2C协议">
                  LV08-01-I2C子系统-01-I2C协议 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2017 – 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">苏木</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">3.7m</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">225:26</span>
  </span>
</div>




    <span id="sitetime"></span>
    <script defer language=javascript>
        function siteTime()
        {
            window.setTimeout("siteTime()", 1000);
            var seconds = 1000;
            var minutes = seconds * 60;
            var hours = minutes * 60;
            var days = hours * 24;
            var years = days * 365;
            var today = new Date();
            var todayYear = today.getFullYear();
            var todayMonth = today.getMonth()+1;
            var todayDate = today.getDate();
            var todayHour = today.getHours();
            var todayMinute = today.getMinutes();
            var todaySecond = today.getSeconds();
            /*==================================================
            Date.UTC() -- 返回date对象距世界标准时间(UTC)1970年1月1日午夜之间的毫秒数(时间戳)
            year        - 作为date对象的年份，为4位年份值
            month       - 0-11之间的整数，做为date对象的月份
            day         - 1-31之间的整数，做为date对象的天数
            hours       - 0(午夜24点)-23之间的整数，做为date对象的小时数
            minutes     - 0-59之间的整数，做为date对象的分钟数
            seconds     - 0-59之间的整数，做为date对象的秒数
            microseconds - 0-999之间的整数，做为date对象的毫秒数
            ==================================================*/
            var t1 = Date.UTC(2017, 
                              5, 
                              19, 
                              0, 
                              0, 
                              0); //北京时间
            var t2 = Date.UTC(todayYear,todayMonth,todayDate,todayHour,todayMinute,todaySecond);
            var diff = t2-t1;
            var diffYears = Math.floor(diff/years);
            var diffDays = Math.floor((diff/days)-diffYears*365);
            var diffHours = Math.floor((diff-(diffYears*365+diffDays)*days)/hours);
            var diffMinutes = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours)/minutes);
            var diffSeconds = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours-diffMinutes*minutes)/seconds);
            document.getElementById("sitetime").innerHTML="已在这里 "+diffYears+" 年 "+diffDays+" 天 "+diffHours+" 小时 "+diffMinutes+" 分钟 "+diffSeconds+" 秒";
        }
        siteTime();
    </script>



    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.umd.js" integrity="sha256-ytMJGN3toR+a84u7g7NuHm91VIR06Q41kMWDr2pq7Zo=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>

  <script src="/js/third-party/pace.js"></script>


  




  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


 
        <div id="click-show-text"
            data-mobile = false
            data-text = 富强,民主,文明,和谐,自由,平等,公正,法制,爱国,敬业,诚信,友善
            data-fontsize = 15px
            data-random= false>
        </div>
       

      
        <script async src=https://cdn.jsdelivr.net/npm/hexo-next-mouse-effect@latest/click/showText.js></script>
      

      
    




    <script async src="/js/fancybox_param.js"></script>





<!-- APlayer本体 -->



</body>
</html>
